<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>点击小球游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f0f8ff;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: #333;
        }
        #game-area {
            width: 500px;
            height: 400px;
            margin: 20px auto;
            border: 3px solid #333;
            position: relative;
            background-color: #e0e0e0;
            overflow: hidden;
        }
        .ball {
            width: 40px;
            height: 40px;
            background-color: #ff4757;
            border-radius: 50%;
            position: absolute;
            cursor: pointer;
            user-select: none;
        }
        #score, #time {
            font-size: 20px;
            margin: 10px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #2ed573;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #7bed9f;
        }
    </style>
</head>
<body>

    <h1>🎯 点击小球游戏</h1>
    <div id="score">得分: 0</div>
    <div id="time">时间: 10 秒</div>
    <button onclick="startGame()">开始游戏</button>

    <div id="game-area"></div>

    <script>
        let score = 0;
        let timeLeft = 10;
        let gameTimer;
        let ballTimer;

        const gameArea = document.getElementById("game-area");
        const scoreDisplay = document.getElementById("score");
        const timeDisplay = document.getElementById("time");

        function startGame() {
            // 重置游戏状态
            score = 0;
            timeLeft = 10;
            scoreDisplay.textContent = "得分: 0";
            timeDisplay.textContent = "时间: 10 秒";
            clearBalls();
            clearInterval(gameTimer);
            clearInterval(ballTimer);

            // 开始倒计时
            gameTimer = setInterval(() => {
                timeLeft--;
                timeDisplay.textContent = `时间: ${timeLeft} 秒`;
                if (timeLeft <= 0) {
                    endGame();
                }
            }, 1000);

            // 每 800ms 出现一个新球
            ballTimer = setInterval(createBall, 800);
        }

        function createBall() {
            const ball = document.createElement("div");
            ball.classList.add("ball");

            const size = Math.random() * 30 + 30; // 30-60px 大小
            const x = Math.random() * (gameArea.offsetWidth - size);
            const y = Math.random() * (gameArea.offsetHeight - size);

            ball.style.width = `${size}px`;
            ball.style.height = `${size}px`;
            ball.style.left = `${x}px`;
            ball.style.top = `${y}px`;

            ball.addEventListener("click", () => {
                score++;
                scoreDisplay.textContent = `得分: ${score}`;
                ball.remove(); // 点中后移除
            });

            gameArea.appendChild(ball);

            // 3秒后自动消失（如果没被点中）
            setTimeout(() => {
                if (ball.parentNode === gameArea) {
                    ball.remove();
                }
            }, 3000);
        }

        function clearBalls() {
            const balls = document.querySelectorAll(".ball");
            balls.forEach(ball => ball.remove());
        }

        function endGame() {
            clearInterval(gameTimer);
            clearInterval(ballTimer);
            clearBalls();
            alert(`游戏结束！你的得分是: ${score}`);
        }
    </script>

</body>
</html>
